import { mapState, mapActions } from 'vuex'

export const defaultState = {
  computed: {
    ...mapState('videoBrowse', {
      treeNodeKey: state => state.treeNodeKey,
      isReplace: state => state.isReplace,
      layoutSelected: state => state.layoutSelected,
      videoLayoutList: state => state.videoLayoutList,
      videoLayoutType: state => state.videoLayoutType,
      videoLayoutTypeList: state => state.videoLayoutTypeList,
      authorizationId: state => state.authorizationId,
      isPtzShow: state => state.isPtzShow,
      layoutConfig: state => state.layoutConfig,
      cameraCheckedList: state => state.cameraCheckedList,
      cameraCheckedKeys: state => state.cameraCheckedKeys,
      cameraPageList: state => state.cameraPageList,
      cameraPage: state => state.cameraPage,
      cameraTotal: state => state.cameraTotal,
      carouselTime: state => state.carouselTime,
      carouselTimer: state => state.carouselTimer,
      carouselSwitch: state => state.carouselSwitch,
      replayTime: state => state.replayTime,
      isManual: state => state.isManual,
      replayPosition: state => state.replayPosition
    })
  },
  methods: {
    ...mapActions('videoBrowse', [
      'changeTreeNodeKey',
      'changeLayoutSelected',
      'changeVideoLayoutType',
      'changeVideoLayoutList',
      'removeVideoPlayer',
      'addVideoPlayer',
      'cutVideoPlayer',
      'setCameraInfo',
      'setReplayInfo',
      'changeReplayTime',
      'changeIsManual',
      'changeReplayPosition',
      'setReplayItemTime',
      'changeIsReplace',
      'changeAuthorizationId',
      'changeLayoutConfig',
      'addCameraList',
      'cutCameraList',
      'cameraChangeState',
      'cameraPagesGroup',
      'cameraCheckedClear',
      'prevCameraPage',
      'nextCameraPage',
      'stopCarousel',
      'changeCarouselSwitch',
      'changeCarouselTime',
      'changeCarouselTimer',
      'changeIsPtzShow'
    ]),
    // 深拷贝
    getNewData(source) {
      const $this = this
      if (!source && typeof source !== 'object') {
        throw new Error('error arguments', 'shallowClone')
      }
      const targetObj = source.constructor === Array ? [] : {}
      Object.keys(source).forEach(keys => {
        if (source[keys] && typeof source[keys] === 'object') {
          targetObj[keys] = source[keys].constructor === Array ? [] : {}
          targetObj[keys] = $this.getNewData(source[keys])
        } else {
          targetObj[keys] = source[keys]
        }
      })
      return targetObj
    },
    setNextSelected() {
      const index = this.videoLayoutList.findIndex(item => {
        return item.uuid === this.layoutSelected.uuid
      })
      if (index < this.videoLayoutList.length - 1) {
        this.changeLayoutSelected(this.videoLayoutList[index + 1].uuid)
      }
      if (index === this.videoLayoutList.length - 1) {
        this.changeLayoutSelected(this.videoLayoutList[index].uuid)
      }
      if (!this.isReplace && this.layoutSelected.cameraInfo.id) {
        this.setNextSelected()
      }
    }
  }
}
